#!/bin/sh
cd ${0%/*} || exit 1    # run from this directory
makeType=${1:-libso}

# get SCOTCH_VERSION, SCOTCH_ARCH_PATH
if settings=`$WM_PROJECT_DIR/bin/foamEtcFile config/scotch.sh`
then
    . $settings
    echo "using SCOTCH_ARCH_PATH=$SCOTCH_ARCH_PATH"
else
    echo
    echo "Error: no config/scotch.sh settings"
    echo
fi


#
# define how to create an mpi-versioned library of $makeType
# compile into qualified directory
# use sentinel file to handle version changes
#
wmakeMpiLib()
{
    set +x
    for libName
    do
    (
        WM_OPTIONS="$WM_OPTIONS$WM_MPLIB"
        whichmpi="$libName/Make/$WM_OPTIONS/using:$FOAM_MPI"
        whichscotch="$libName/Make/$WM_OPTIONS/using:$SCOTCH_VERSION"
        [ -e "$whichmpi" -a -e "$whichscotch" ] || wclean $libName
        echo "wmake $makeType $libName"
        wmake $makeType $libName
        touch "$whichmpi" "$whichscotch"
    )
    done
    set -x
}

set -x

wmakeLnInclude decompositionMethods

if [ -n "$SCOTCH_ARCH_PATH" ]
then
    wmake $makeType scotchDecomp

    if [ -d "$FOAM_LIBBIN/$FOAM_MPI" ]
    then
        #- Bit of a hack: ptscotch 6 requires scotch linked as well as. Can be
        #  removed once ptscotch declares dependency on scotch itself.
        if [ "$SCOTCH_VERSION" = "scotch_6.0.0" ]
        then
            export LINK_FLAGS="-lscotch"
        fi

        wmakeMpiLib ptscotchDecomp
    fi
else
    echo
    echo "Skipping scotchDecomp (ptscotchDecomp)"
    echo
fi

wmake $makeType decompositionMethods

wmake $makeType decompose

# ----------------------------------------------------------------- end-of-file
